package com.feinno.urcs.group.api;

import com.feinno.urcs.group.entity.*;
import com.feinno.urcs.types.MobileNo;
import org.helium.framework.annotations.ServiceInterface;

import java.sql.SQLException;
import java.util.List;


/**
 * GRPDB
 * GRP_GroupInfo
 * GRP_GroupMemeber
 * <p>
 * <p>
 * Created by Lei Gao on 4/22/15.
 */
@ServiceInterface(id = "group:GroupDao")
public interface GroupDao {

	/**
	 * select * from GRP_GroupInfo where GroupIndex = @GroupIndex
	 *
	 * @param index
	 * @return
	 */
	GroupInfo getGroupInfo(GroupIndex index) throws Exception;


	/**
	 * 1. insert into GRP_GroupInfo(groupUri, conversationId, subject, crateor, ...)
	 * 2. for (groupmobileNos) {
	 * insert into GRP_GroupMember(groupUri, mobileNo, ...)
	 * }
	 *
	 * @param index
	 * @param adminMobileNo
	 * @param conversationId
	 * @param subject
	 * @param groupMembers
	 */
	void createGroup(GroupIndex index, MobileNo adminMobileNo, String conversationId, String subject, List<MobileNo> groupMembers) throws Exception;


	/**
	 * select @count = count(*) from GRP_GroupMember where GroupIndex = @GroupIndex and status in (1, 2, 3)
	 * if (@count < MAX_GROUP_CAPACITY) {
	 * update GRP_GroupMember set status = 3 where ... (groupUri, mobileNo)
	 * return true;
	 * } else {
	 * do nothing
	 * return false;   // 开启逗你玩模式，无限403直到BYE删除
	 * }
	 *
	 * @param index
	 * @param mobileNo
	 * @return
	 */
	boolean acceptGroupMemberInvitation(GroupIndex index, MobileNo mobileNo) throws Exception;

	/**
	 * /**
	 * 1. 客户端回拒绝
	 * 2. 逗你玩模式客户端BYE
	 * 3. 移除成员
	 * 4. 成员主动退出
	 * delete from GRP_GroupMember where GroupIndex = ... and mobileNo = ...
	 *
	 * @param index
	 * @param mobileNo
	 * @remark 无法移除超管
	 */
	void deleteGroupMember(GroupIndex index, MobileNo mobileNo) throws Exception;

	/**
	 * @param index
	 * @param nickname
	 */
	void updateGroupMemberNickname(GroupIndex index, MobileNo mobileNo, String nickname) throws Exception;


	/**
	 * 返回还能邀请的成员数量，已满返回"0"
	 *
	 * @param index
	 * @return
	 */
	int getGroupMemberLimit(GroupIndex index) throws Exception;

	/**
	 * for (mobileNos) {
	 * insert into GRP_GroupMember (...)
	 * }
	 *
	 * @param index
	 * @param groupMember
	 */
	int inviteGroupMember(GroupIndex index, MobileNo referBy, MobileNo groupMember) throws Exception;

	/**
	 * @param index
	 * @param originMobileNo
	 * @param targetMobileNo
	 */
	void changeGroupAdmin(GroupIndex index, MobileNo originMobileNo, MobileNo targetMobileNo) throws Exception;

	/**
	 * @param index
	 */
	void dismissGroup(GroupIndex index) throws Exception;

	/**
	 * @param index
	 * @param subject
	 */
	void updateGroupSubject(GroupIndex index, String subject) throws Exception;

	/**
	 * is group chairman
	 *
	 * @param index
	 * @param mobileNo
	 * @return
	 * @throws SQLException
	 */
	boolean isChairMan(GroupIndex index, MobileNo mobileNo) throws Exception;

	/**
	 * 选举一个管理员, 并返回MobileNo
	 *
	 * @param index
	 * @return
	 * @throws Exception
	 */
	MobileNo appointAdmin(GroupIndex index) throws Exception;

}
